package com.ruoyi.project.business.gpiobiz.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.iotchip.entity.Chip;
import com.ruoyi.project.business.iotchip.mapper.ChipMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.gpiobiz.mapper.ControlGpioBizMapper;
import com.ruoyi.project.business.gpiobiz.entity.ControlGpioBiz;
import com.ruoyi.project.business.gpiobiz.request.ListControlGpioBizRequest;
import com.ruoyi.project.business.gpiobiz.request.AddControlGpioBizRequest;
import com.ruoyi.project.business.gpiobiz.response.ControlGpioBizResponse;
import com.ruoyi.project.business.gpiobiz.service.IControlGpioBizService;

/**
 * 设备引脚业务Service业务层处理
 * 
 * @author ${author}
 * @date 2024-07-25
 */
@Service
public class ControlGpioBizServiceImpl implements IControlGpioBizService {
    @Autowired
    private ControlGpioBizMapper controlGpioBizMapper;
    @Autowired
    private ChipMapper chipMapper;

    /**
     * 查询设备引脚业务
     * 
     * @return 设备引脚业务
     */
    @Override
    public ControlGpioBiz selectControlGpioBizById(Long id) {
        return controlGpioBizMapper.selectControlGpioBizById(id);
    }

    /**
     * 查询设备引脚业务列表
     * 
     * @return 设备引脚业务
     */
    @Override
    public List<ControlGpioBizResponse> selectControlGpioBizList(ListControlGpioBizRequest request, Long tenantId) {
        // TODO request to  param
        ControlGpioBiz param = BeanUtils.copy(request, ControlGpioBiz.class);
        param.setTenantId(tenantId);
        List<ControlGpioBiz> entitis = controlGpioBizMapper.selectControlGpioBizList(param);
        List<ControlGpioBizResponse> resList = BeanUtils.copyList(entitis, ControlGpioBizResponse.class);
        return resList;
    }

    /**
     * 查询设备引脚业务列表
     *
     * @return 设备引脚业务
     */
    @Override
    public List<ControlGpioBizResponse> selectListWithMac(ListControlGpioBizRequest request, Long tenantId) {
        ControlGpioBiz param = BeanUtils.copy(request, ControlGpioBiz.class);
        param.setTenantId(tenantId);
        List<ControlGpioBiz> entitis = controlGpioBizMapper.selectControlGpioBizList(param);
        List<ControlGpioBizResponse> resList = BeanUtils.copyList(entitis, ControlGpioBizResponse.class);
        // 填充 mac
        List<Long> chipIds = resList.stream().map(ControlGpioBizResponse::getChipId).distinct().collect(Collectors.toList());
        List<Chip> chips = chipMapper.selectChipByIds(chipIds);

        resList.forEach(res -> {
            Optional<Chip> opt = chips.stream().filter(chip -> chip.getId().equals(res.getChipId())).findFirst();
            if(opt.isPresent()) {
                res.setMac(opt.get().getMac());
            }
        });

        return resList;
    }

    /**
     * 新增设备引脚业务
     * 
     * @return 结果
     */
    @Override
    public int insertControlGpioBiz(AddControlGpioBizRequest request, Long operatorId, Long tenantId) {
        ControlGpioBiz controlGpioBiz = this.selectExist(request.getChipId(), request.getGpio());
        if(controlGpioBiz == null) {
            ControlGpioBiz entity = BeanUtils.copy(request, ControlGpioBiz.class);
            entity.setTenantId(tenantId);
            entity.setCreator(operatorId);
            entity.setDeleted(Boolean.FALSE);
            entity.setCreateTime(DateUtils.getNowDate());
            entity.setOnOff(0);
            return controlGpioBizMapper.insertControlGpioBiz(entity);
        } else {
            return controlGpioBiz.getId().intValue();
        }
    }

    private ControlGpioBiz selectExist(Long chipId, Integer gpio) {
        ControlGpioBiz param = new ControlGpioBiz();
        param.setChipId(chipId);
        param.setGpio(gpio);

        List<ControlGpioBiz> controlGpioBizs = controlGpioBizMapper.selectControlGpioBizList(param);
        if(CollectionUtils.isEmpty(controlGpioBizs)) {
            return null;
        }
        return controlGpioBizs.get(0);
    }

    /**
     * 修改设备引脚业务
     * 
     * @return 结果
     */
    @Override
    public int updateControlGpioBiz(ControlGpioBiz entity, Long operatorId, Long tenantId) {
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return controlGpioBizMapper.updateControlGpioBiz(entity);
    }

    /**
     * 批量删除设备引脚业务
     * 
     * @return 结果
     */
    @Override
    public int deleteControlGpioBizByIds(Long[] ids, Long operatorId, Long tenantId) {
        return controlGpioBizMapper.deleteControlGpioBizByIds(ids);
    }

    /**
     * 删除设备引脚业务信息
     * 
     * @return 结果
     */
    @Override
    public int deleteControlGpioBizById(Long id, Long operatorId, Long tenantId) {
        return controlGpioBizMapper.deleteControlGpioBizById(id);
    }
}
